namespace AgentJohnson
{
  using System;
  using System.Linq;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Feature.Services.CSharp.Bulbs;
  using Resharper.Abstractions.Actions;
  using Resharper.Abstractions.CodeDom.Expressions.InvocationExpressions;
  using Resharper.Abstractions.CodeDom.TypeMembers.Methods;

  /// <summary>Defines the <see cref="PullParameters"/> class.</summary>
  [ContextAction("Pull parameters [Agent Johnson]", "Pulls the containing methods parameters to this method call.")]
  public class PullParameters : ContextActionBase
  {
    #region Constructors and Destructors

    /// <summary>Initializes a new instance of the <see cref="PullParameters"/> class.</summary>
    /// <param name="provider">The provider.</param>
    public PullParameters([NotNull] ICSharpContextActionDataProvider provider) : base(provider)
    {
    }

    #endregion

    #region Methods

    /// <summary>Determines whether this instance can execute the specified action context.</summary>
    /// <param name="context">The action context.</param>
    /// <returns><c>true</c> if this instance can execute the specified action context; otherwise, <c>false</c>.</returns>
    protected override bool CanExecute(CanExecuteContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      return this.GetModel(context) != null;
    }

    /// <summary>Executes the action.</summary>
    /// <param name="context">The execute context.</param>
    protected override void Execute(ExecuteContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var model = this.GetModel(context);
      if (model == null)
      {
        return;
      }

      foreach (var parameter in model.ContainingMethod.Parameters.Reverse())
      {
        var argument = context.Factory.CreateArgument(parameter.Name);

        model.InvocationExpression.AddArgumentAfter(argument, null);
      }
    }

    /// <summary>Gets the text.</summary>
    /// <returns>Returns the text.</returns>
    protected override string GetText()
    {
      return "Pull parameters [Agent Johnson]";
    }

    /// <summary>Gets the model.</summary>
    /// <param name="context">The context.</param>
    /// <returns>Returns the model.</returns>
    private Model GetModel([NotNull] ProviderContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var invocationExpression = context.GetSelectedInvocationExpression();
      if (invocationExpression == null)
      {
        return null;
      }

      if (invocationExpression.Arguments.Any())
      {
        return null;
      }

      var method = context.GetContainingMethod();
      if (method == null)
      {
        return null;
      }

      if (!method.Parameters.Any())
      {
        return null;
      }

      var invokedElement = invocationExpression.InvokedElement;
      if (!invokedElement.Parameters.Any())
      {
        return null;
      }

      return new Model
      {
        InvocationExpression = invocationExpression,
        ContainingMethod = method
      };
    }

    #endregion

    /// <summary>Defines the <see cref="Model"/> class.</summary>
    private class Model
    {
      #region Properties

      /// <summary>
      /// Gets or sets the invocation expression.
      /// </summary>
      /// <value>The invocation expression.</value>
      [NotNull]
      public InvocationExpression InvocationExpression { get; set; }

      /// <summary>
      /// Gets or sets the method.
      /// </summary>
      /// <value>The method.</value>
      [NotNull]
      public Method ContainingMethod { get; set; }

      #endregion
    }
  }
}